package com.haircut.security.service;

import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.haircut.enums.UserType;
import com.haircut.mapper.WechatUser;
import com.haircut.mapper.WechatUserMapper;
import com.haircut.security.model.SysUserDetails;
import lombok.RequiredArgsConstructor;
import org.apache.commons.lang3.ObjectUtils;
import org.springframework.security.core.authority.SimpleGrantedAuthority;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.security.core.userdetails.UserDetailsService;
import org.springframework.security.core.userdetails.UsernameNotFoundException;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.util.Arrays;
import java.util.stream.Collectors;

/**
 * 系统用户认证
 *

 */
@Service
@RequiredArgsConstructor
public class SysUserDetailsService implements UserDetailsService {
    private final WechatUserMapper wechatUserMapper;

    @Override
    public UserDetails loadUserByUsername(String openid) throws UsernameNotFoundException {
        WechatUser byOpenid = wechatUserMapper.selectOne(new LambdaUpdateWrapper<WechatUser>()
                .eq(WechatUser::getOpenid, openid));
        if (byOpenid == null) {
            throw new UsernameNotFoundException(openid);
        }
        SysUserDetails sysUserDetails = new SysUserDetails();
        sysUserDetails.setOpenId(openid);
        sysUserDetails.setUserId(byOpenid.getId());
        return sysUserDetails;
    }

    public UserDetails loadUserByWxOpenid(String mobile, String wxOpenid) {
        WechatUser byOpenid = wechatUserMapper.selectOne(new LambdaUpdateWrapper<WechatUser>()
                .eq(WechatUser::getPhone, mobile));
        SysUserDetails sysUserDetails = new SysUserDetails();
        if (byOpenid == null) {
            WechatUser wechatUser = new WechatUser();
            wechatUser.setUserType(UserType.CUSTOMER.getCode());
            wechatUser.setOpenid(wxOpenid);
            wechatUser.setPhone(mobile);
            if(StrUtil.isNotBlank(mobile)){
                wechatUser.setNickname("顾客"+mobile.substring(7));
            }
            wechatUser.setBalance(BigDecimal.ZERO);
            wechatUserMapper.insert(wechatUser);
            sysUserDetails.setUserId(wechatUser.getId());
            SimpleGrantedAuthority simpleGrantedAuthority = new SimpleGrantedAuthority("ROLE_" + UserType.CUSTOMER.getCode());
            sysUserDetails.setAuthorities(Arrays.asList(simpleGrantedAuthority));
        }else {
            if(ObjectUtils.isEmpty(byOpenid.getOpenid())){
                UpdateWrapper<WechatUser> updateWrapper = new UpdateWrapper<>();
                updateWrapper.lambda().eq(WechatUser::getPhone,mobile)
                        .set(WechatUser::getOpenid, wxOpenid);
                wechatUserMapper.update(updateWrapper);
            }
            SimpleGrantedAuthority simpleGrantedAuthority = new SimpleGrantedAuthority("ROLE_" + UserType.getByCode(byOpenid.getUserType()).getCode());
            sysUserDetails.setAuthorities(Arrays.asList(simpleGrantedAuthority));
            sysUserDetails.setUserId(byOpenid.getId());
        }
        sysUserDetails.setOpenId(wxOpenid);
        return sysUserDetails;
    }
}
